<template>
	<!-- 使用 page-meta 组件配合阻止滚动穿透 ,page-meta一定作为页面的第一个节点-->
	<page-meta :page-style="'overflow:'+(show?'hidden':'visible')"></page-meta>

	<uni-popup ref="addPopup" type="bottom" @change="changePop" :safeArea="false">
		<my-add-fridge-pop @close="addPopClose"></my-add-fridge-pop>
	</uni-popup>

	<view class="device-container">
		<view class="tips-view" v-if="fridge_list.length===0">
			<image src="../../../static/empty_img/no-data.png"></image>
			<text>您暂未绑定冰箱，可点击右下方按钮进行添加</text>
		</view>

		<view class="fridge-list" v-if="token">
			<!-- 已绑定的冰箱列表 -->
			<my-fridge-list :fridge_list="fridge_list" :defautl_fid="default_fridge.fid" @detail="gotoDetail">
			</my-fridge-list>
		</view>

		<!-- 添加冰箱 -->
		<uni-fab ref="fab" :pattern="pattern" :horizontal="horizontal" :vertical="vertical" @fabClick="addfridge" />
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'

	export default {
		data() {
			return {
				// 悬浮按钮
				horizontal: 'right',
				vertical: 'bottom',
				pattern: {
					buttonColor: '#10aeff',
					iconColor: '#fff'
				},
				show: false
			}
		},
		computed: {
			// 2. 从 user 模块中导入需要的 token 字符串
			...mapState('user', ['token']),
			...mapState('fridge', ['fridge_list', 'default_fridge'])
		},

		onLoad() {
			this.load()
		},

		onPullDownRefresh() {
			uni.$api.fridge.getAll()
			this.load()
			uni.stopPullDownRefresh()
		},

		methods: {

			load() {
				if (this.token) {
					uni.showLoading({
						title: '加载中...',
					})
					setTimeout(() => {
						uni.hideLoading()
					}, 500)

					// console.log('冰箱列表：');
					// console.log(this.fridge_list);
				} else {
					uni.$showMsg('尚未登录')
				}
			},

			// 跳转
			gotoAddFridge() {
				this.$refs.addPopup.close()
				// 如果未登录
				if (!this.token)
					return uni.$api.user.login()
				uni.navigateTo({
					url: '/pagesSub/pageFridge/select_brand/select_brand'
				})
			},
			gotoDetail(obj) {
				uni.navigateTo({
					url: '/pagesSub/pageFridge/fridge_detail/fridge_detail?fridge=' + JSON.stringify(obj)
				})
			},

			//请求

			// 事件处理
			changePop(e) {
				this.show = e.show
			},
			addPopClose() {
				this.$refs.addPopup.close()
			},
			addfridge() {
				this.$refs.addPopup.open()
			},
			scan() {
				this.$refs.addPopup.close()
				// 允许从相机和相册扫码
				uni.scanCode({
					scanType: ['qrCode'],
					success: (res) => {
						console.log('条码内容：' + res.result);
					}
				});
			}

		},
	}
</script>
<style lang="scss">
	.device-container {
		display: flex;
		flex-direction: column;

		.tips-view {
			display: flex;
			flex-direction: column;
			align-items: center;
			margin-top: 200rpx;
			color: #2979FF;
		}
	}
</style>